//
//  HYNickNameViewController.m
//  HaiYue
//
//  Created by tucici on 2019/3/24.
//  Copyright © 2019 skyline. All rights reserved.
//

#import "HYNickNameViewController.h"

@interface HYNickNameViewController ()<HYTextFieldDelegate>
@property (nonatomic, strong) HYTextField *oldNameTextField;
@property (nonatomic, strong) HYTextField *textfield;
@property (nonatomic, strong) HYButton *mainBtn;

@end

@implementation HYNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改昵称";
    self.view.backgroundColor = CELL_LINE_COLOR;
    // Do any additional setup after loading the view.
}
- (HYTextField *)oldNameTextField
{
    if (!_oldNameTextField)
    {
        _oldNameTextField = [[HYTextField alloc]initWithPlaceholder:@""];
        _oldNameTextField.text = self.oldName;
        _oldNameTextField.userInteractionEnabled = NO;
        [self.view addSubview:_oldNameTextField];
        [_oldNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(GAP);
            make.left.equalTo(self.view).with.offset(GAP);
            make.right.equalTo(self.view).with.offset(-GAP);
            make.height.mas_equalTo(TextFieldHeight);
        }];
    }
    return _oldNameTextField;
}

- (HYTextField *)textfield
{
    if (!_textfield)
    {
        _textfield = [[HYTextField alloc]initWithPlaceholder:@""];
        _textfield.returnKeyType = UIReturnKeyDone;
        _textfield.hydelegate = self;
        [self.view addSubview:_textfield];
        [_textfield mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.oldNameTextField.mas_bottom).with.offset(GAP);
            make.left.equalTo(self.view).with.offset(GAP);
            make.right.equalTo(self.view).with.offset(-GAP);
            make.height.mas_equalTo(TextFieldHeight);
        }];
    }
    return _textfield;
}
- (HYButton *)mainBtn
{
    if (!_mainBtn)
    {
        _mainBtn = [[HYButton alloc]initWithTitle:@"修改" backColor:THEME_COLOR];
        [_mainBtn addTarget:self action:@selector(actionForMainButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_mainBtn];
        [_mainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textfield.mas_bottom).with.offset(GAP);
            make.left.equalTo(self.view).with.offset(GAP);
            make.right.equalTo(self.view).with.offset(-GAP);
            make.height.mas_equalTo(ButtonHeight);
        }];
    }
    return _mainBtn;
}
- (void)actionForMainButton
{
    if ([self.textfield.text length] < 1 )
        return [CCAlert showMessage:@"昵称不能为空" hidden:nil];
    else if ([self.textfield.text length] > 12 )
        return  [CCAlert showMessage:@"昵称不能多于12字" hidden:nil];
    [self startHUD];
    
    [CCHttp post:API_NICKMAN_UPDATE para:@{@"nickname":self.textfield.text} success:^(id  _Nonnull response) {
        [self endHUD];
        [self.navigationController popViewControllerAnimated:YES];
    } fail:^(NSError * _Nonnull error) {
        [self endHUD];
    }];
//    [HYHttps post:API_NICKMAN_UPDATE para:@{@"nickname":self.textfield.text} complete:^(id response) {
//        [self endHUD];
//        [self.navigationController popViewControllerAnimated:YES];
//    } fail:^(NSDictionary *dict) {
//        [self endHUD];
//
//    }];
}
#pragma mark HYTextFieldDelegate事件
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string containsString:@" "]) return NO;
    if (![string isChinese] && ![string isNumberOrAlphabet] && ![string isEqualToString:@""]) return NO;
    return YES;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.style = HYViewNaviBarStyleOnlyLeftBtn;
    self.oldNameTextField.hidden = NO;
    self.textfield.hidden = NO;
    self.mainBtn.hidden = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
